﻿using IMES.Common.Domain.KeepModel.ProductionFile;
using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using Keep.API.Common.L.Dtos;
using MediatR;

namespace Keep.API.Appliction.ProductionPlanFile
{
    /// <summary>
    /// 产品打印
    /// </summary>
    public class ProductPlanPrintHandler:IRequestHandler<ProductPlanPrintCommon,Result>
    {
        private readonly IFreeSql _freeSql;

        /// <summary>
        /// 产品打印
        /// </summary>
        public ProductPlanPrintHandler(IFreeSql freeSql)
        {
            _freeSql= freeSql;
        }

        public Task<Result> Handle(ProductPlanPrintCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CodeEnum.success, Messege = "查询成功" };
            PrintDto printdtos = new PrintDto();
            var list = _freeSql.Select<PlanProductDetails,Product>()
                .InnerJoin((p, pd) => p.ProductNumber == pd.ProductNumber)
                 .WhereIf(!string.IsNullOrEmpty(request.DocumentNumber), (p, pd) => p.DocumentNumber == request.DocumentNumber)
                .ToList((p, pd) => new PrintDtos()
                {
                    ProductNumber = pd.ProductNumber,
                    ProductName = pd.ProductName,
                    ProductSpecification = pd.ProductSpecification,
                    Quantity = p.Quantity
                });
            var plan = _freeSql.Select<PlanProductDetails>().WhereIf(!string.IsNullOrEmpty(request.DocumentNumber), p => p.DocumentNumber == request.DocumentNumber).First();

            printdtos.pOrderNo = plan.OrderNo;
            printdtos.pName = plan.CreateName;
            printdtos.createTime = plan.CreateDate;
            printdtos.PrintDtos = list;
            result.Data = printdtos;
             
            return Task.FromResult(result);


        }
    }
}
